/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLNV.Bean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import java.sql.*;
import java.util.*;
import QLNV.Entity.*;
import QLNV.Utilities.*;

/**
 *
 * @author Ice
 */
public class NhanVienForm extends org.apache.struts.action.ActionForm {

    /**
     *
     */
    public NhanVienForm() {
        super();
        // TODO Auto-generated constructor stub
    }
    private String manv;
    private String tennv;
    private String gioitinh;
    private String diachi;
    private String email;
    private String chucvu;
    private String mapban;
    private String maluong;
    private String noicongtac;
    private String message;
    private String tenpban;
    private String ngaysinh;

    /**
     * @return the manv
     */
    public String getManv() {
        return manv;
    }

    /**
     * @param manv the manv to set
     */
    public void setManv(String manv) {
        this.manv = manv;
    }

    /**
     * @return the tennv
     */
    public String getTennv() {
        return tennv;
    }

    /**
     * @param tennv the tennv to set
     */
    public void setTennv(String tennv) {
        this.tennv = tennv;
    }

    /**
     * @return the gioitinh
     */
    public String getGioitinh() {
        return gioitinh;
    }

    /**
     * @param gioitinh the gioitinh to set
     */
    public void setGioitinh(String gioitinh) {
        this.gioitinh = gioitinh;
    }

    /**
     * @return the diachi
     */
    public String getDiachi() {
        return diachi;
    }

    /**
     * @param diachi the diachi to set
     */
    public void setDiachi(String diachi) {
        this.diachi = diachi;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the chucvu
     */
    public String getChucvu() {
        return chucvu;
    }

    /**
     * @param chucvu the chucvu to set
     */
    public void setChucvu(String chucvu) {
        this.chucvu = chucvu;
    }

    /**
     * @return the mapban
     */
    public String getMapban() {
        return mapban;
    }

    /**
     * @param mapban the mapban to set
     */
    public void setMapban(String mapban) {
        this.mapban = mapban;
    }

    /**
     * @return the maluong
     */
    public String getMaluong() {
        return maluong;
    }

    /**
     * @param maluong the maluong to set
     */
    public void setMaluong(String maluong) {
        this.maluong = maluong;
    }

    /**
     * @return the noicongtac
     */
    public String getNoicongtac() {
        return noicongtac;
    }

    /**
     * @param noicongtac the noicongtac to set
     */
    public void setNoicongtac(String noicongtac) {
        this.noicongtac = noicongtac;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    public ArrayList<NhanVien> view() throws SQLException {
        ArrayList<NhanVien> nVienList = new ArrayList<NhanVien>();
        try {
//            DBHelper.getInstance().initConnection();
//            ResultSet rs = DBHelper.getInstance().executeQueryProc("View_Project", null);
            Conn cnn = new Conn();
            String sql = "select * from v_NhanVien";
            ResultSet rs = cnn.exec(sql);
            if (rs != null) {
                while (rs.next()) {
                    NhanVien nv = new NhanVien();
                    nv.setManv(rs.getString("manv"));
                    nv.setChucvu(rs.getString("chucvu"));
                    nv.setDiachi(rs.getString("diachi"));
                    nv.setEmail(rs.getString("email"));
                    nv.setGioitinh(rs.getString("gioitinh"));
                    nv.setMaluong(rs.getString("maluong"));

                    PhongBan pban = new PhongBan();
                    pban.setTenPban(rs.getString("tenpban"));
                    pban.setMaPban(rs.getString("mapban"));
                    nv.setPhongban(pban);

                    nv.setTennv(rs.getString("tennv"));
                    nv.setNgaysinh(rs.getString("ngaysinh"));
                    nv.setNoicongtac(rs.getString("noicongtac"));
                    nVienList.add(nv);
                }
            }
            cnn.close();
        } catch (SQLException e) {
            e.getMessage();
        }
        return nVienList;
    }

    /**
     * detail
     * @param manv
     * @return
     * @throws SQLException 
     */
    public ArrayList<NhanVien> detail(String manv) throws SQLException {
        ArrayList<NhanVien> nVienList = new ArrayList<NhanVien>();
        try {
            Conn cnn = new Conn();
            String sql = "select * from v_NhanVien where manv='" + manv + "'";
            ResultSet rs = cnn.exec(sql);
            if (rs != null) {
                while (rs.next()) {
                    NhanVien nv = new NhanVien();
                    nv.setManv(rs.getString("manv"));
                    nv.setChucvu(rs.getString("chucvu"));
                    nv.setDiachi(rs.getString("diachi"));
                    nv.setEmail(rs.getString("email"));
                    nv.setGioitinh(rs.getString("gioitinh"));
                    nv.setMaluong(rs.getString("maluong"));

                    PhongBan pban = new PhongBan();
                    pban.setTenPban(rs.getString("tenpban"));
                    pban.setMaPban(rs.getString("mapban"));
                    nv.setPhongban(pban);

                    nv.setTennv(rs.getString("tennv"));
                    nv.setNgaysinh(rs.getString("ngaysinh"));
                    nv.setNoicongtac(rs.getString("noicongtac"));
                    nVienList.add(nv);
                }
            }
            cnn.close();
        } catch (SQLException e) {
            e.getMessage();
        }
        return nVienList;
    }

    /**
     * insert
     * @param manv
     * @param mapban
     * @param maluong
     * @param tennv
     * @param gioitinh
     * @param ngaysinh
     * @param diachi
     * @param email
     * @param noicongtac
     * @param chucvu
     * @return
     * @throws SQLException 
     */
    public String insert(String manv, String mapban, String maluong, String tennv, String gioitinh, String ngaysinh, String diachi, String email, String noicongtac, String chucvu) throws SQLException {
        String message = "";
        try {
            DBHelper.getInstance().initConnection();
            ArrayList<String> arr = new ArrayList<String>();

            arr.add(manv);
            arr.add(mapban);
            arr.add(maluong);
            arr.add(tennv);
            arr.add(ngaysinh);
            arr.add(gioitinh);
            arr.add(noicongtac);
            arr.add(chucvu);
            arr.add(diachi);
            arr.add(email);
            message = DBHelper.getInstance().executeUpdateProcReturn("Insert_NhanVien", arr);
            DBHelper.getInstance().closeConnection();
        } catch (Exception e) {
            e.getMessage();
        }
        return message;
    }

    /**
     * delete
     * @param manv
     * @return
     * @throws SQLException 
     */
    public int delete(String manv) throws SQLException {
        Conn cnn = new Conn();
        String sql_delete = "delete from nhanvien where manv='" + manv + "'";
        int cnt = cnn.delete(sql_delete);
        return cnt;
    }

    /**
     * update
     * @param mapban
     * @param tenpban
     * @return
     * @throws SQLException 
     */
    public ArrayList<NhanVien> update(String manv, String mapban, String maluong, String tennv, String ngaysinh, String gioitinh, String chucvu, String noicongtac, String diachi, String email) throws SQLException {
        DBHelper.getInstance().initConnection();
        ArrayList<String> arr = new ArrayList<String>();
        ArrayList<NhanVien> nvList  = new ArrayList<NhanVien>();
        arr.add(manv);
        arr.add(mapban);
        arr.add(maluong);
        arr.add(gioitinh);
        arr.add(diachi);
        arr.add(email);
        arr.add(chucvu);
        arr.add(noicongtac);
        arr.add(ngaysinh);
        arr.add(tennv);
        
        //String message = DBHelper.getInstance().executeUpdateProcReturn("Update_NhanVien", arr);
        ResultSet rs = DBHelper.getInstance().executeQueryProc("Update_NhanVien", arr);
        if (rs != null) {
            while (rs.next()) {
                NhanVien nv = new NhanVien();
                nv.setManv(rs.getString("manv"));
                nv.setChucvu(rs.getString("chucvu"));
                nv.setDiachi(rs.getString("diachi"));
                nv.setEmail(rs.getString("email"));
                nv.setGioitinh(rs.getString("gioitinh"));
                nv.setMaluong(rs.getString("maluong"));

                PhongBan pban = new PhongBan();
                pban.setTenPban(rs.getString("tenpban"));
                pban.setMaPban(rs.getString("mapban"));
                nv.setPhongban(pban);

                nv.setTennv(rs.getString("tennv"));
                nv.setNgaysinh(rs.getString("ngaysinh"));
                nv.setNoicongtac(rs.getString("noicongtac"));
                nvList.add(nv);
            }
        }
        return nvList;
    }

    /**
     * search
     * @param mapban
     * @return
     * @throws SQLException 
     */
    public ArrayList<NhanVien> search(String manv) throws SQLException {
        NhanVien nv = new NhanVien();
        ArrayList<NhanVien> nvList = new ArrayList<NhanVien>();
        DBHelper.getInstance().initConnection();
        ArrayList<String> arr = new ArrayList<String>();
        arr.add(manv);
        ResultSet rs = DBHelper.getInstance().executeQueryProc("Search_NhanVien", arr);
        if (rs != null) {
            while (rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                int numberOfColumns = rsmd.getColumnCount();
                if (numberOfColumns == 1) {
                    nv.setMessage(rs.getString("message"));
                } else {
                    PhongBan pban = new PhongBan();
                    pban.setMaPban(rs.getString("mapban"));
                    pban.setTenPban(rs.getString("tenpban"));
                    nv.setPhongban(pban);

                    nv.setMapban(rs.getString("mapban"));
                    nv.setChucvu(rs.getString("chucvu"));
                    nv.setDiachi(rs.getString("diachi"));
                    nv.setEmail(rs.getString("email"));
                    nv.setGioitinh(rs.getString("gioitinh"));
                    nv.setMaluong(rs.getString("maluong"));
                    nv.setNgaysinh(rs.getString("ngaysinh"));
                    nv.setNoicongtac(rs.getString("noicongtac"));
                    nv.setTennv(rs.getString("tennv"));
                    nv.setManv(rs.getString("manv"));
                    nvList.add(nv);
                }
            }
        }
        return nvList;
        //String message = DBHelper.getInstance().executeUpdateProcReturn("Search_PhongBan", arr);

    }

    /**
     * @return the tenpban
     */
    public String getTenpban() {
        return tenpban;
    }

    /**
     * @param tenpban the tenpban to set
     */
    public void setTenpban(String tenpban) {
        this.tenpban = tenpban;
    }

    /**
     * @return the ngaysinh
     */
    public String getNgaysinh() {
        return ngaysinh;
    }

    /**
     * @param ngaysinh the ngaysinh to set
     */
    public void setNgaysinh(String ngaysinh) {
        this.ngaysinh = ngaysinh;
    }
}
